package top.bulk.encrypt.util;

import cn.hutool.core.annotation.AnnotationUtil;
import top.bulk.encrypt.annotation.Encrypt;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * 注解工具类
 *
 * @author 散装java
 * @date 2024-07-18
 */
public class AnnUtil {
    /**
     * 判断类是否有注解
     *
     * @param clazz        类
     * @param encryptClass 注解
     * @return true/false
     */
    public static boolean hasAnnotation(Class<?> clazz, Class<? extends Annotation> encryptClass) {
        // 获取类的所有字段
        Field[] fields = clazz.getDeclaredFields();
        // 遍历所有字段，检查是否存在 Encrypt 注解
        for (Field field : fields) {
            if (AnnotationUtil.hasAnnotation(field, encryptClass)) {
                return true;
            }
        }
        // 如果当前类有父类，递归检查父类
        if (clazz.getSuperclass() != null) {
            return hasAnnotation(clazz.getSuperclass(), encryptClass);
        }
        return false;
    }

    public static boolean hasAnnotation(Field field, Class<? extends Annotation> encryptClass) {
        return AnnotationUtil.hasAnnotation(field, encryptClass);
    }
}
